/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Jojo
 */
public class XmlManager {

    void read() {
        try {

            File fXmlFile = new File("D:" + File.separator + "test.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());


            System.out.println("----------------------------");
            NodeList nList = doc.getFirstChild().getChildNodes();

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);



                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    System.out.println("\nCurrent Element :" + nNode.getNodeName());

                    Element eElement = (Element) nNode;

                    System.out.println("Staff id : " + eElement.getAttribute("id"));
                    System.out.println("First Name : " + eElement.getAttribute("port"));


                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateTest() {
        try {
            String filepath = "D:" + File.separator + "test.xml";
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(filepath);
            doc.getDocumentElement().normalize();
            
            Node root = doc.getFirstChild();
            System.out.println("Root :" + root.getNodeName());
            NodeList rootlist = root.getChildNodes();
            for (int temp = 0; temp < rootlist.getLength(); temp++) {

                Node nNode = rootlist.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    eElement.setIdAttribute("id", true);
                    System.out.println("id : " + eElement.getAttribute("id"));
                    
                    System.out.println("port : " + eElement.getAttribute("port"));
                    System.out.println("id setzen ging klar");
                }
            }

             
                        
            Element found = doc.getElementById("1");
            found.setAttribute("port", "66");
       

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(filepath));
            transformer.transform(source, result);

            System.out.println("Done");

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (TransformerException tfe) {
            tfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SAXException sae) {
            sae.printStackTrace();
        }

    }
    
    public void writeXmlFile(){
        try {
 
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
 
		// root elements
		Document doc = docBuilder.newDocument();
		Element rootElement = doc.createElement("lejosObjekte");
		doc.appendChild(rootElement);
 
		// staff elements
		Element staff = doc.createElement("farbsensor");
		rootElement.appendChild(staff);
 
		// set attribute to staff element
		Attr attr = doc.createAttribute("id");
		attr.setValue("1");
		staff.setAttributeNode(attr);
                
                Attr port = doc.createAttribute("port");
		port.setValue("99");
		staff.setAttributeNode(port);
 
		 
		// write the content into xml file
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(new File("D:\\file.xml"));
 
		// Output to console for testing
		// StreamResult result = new StreamResult(System.out);
 
		transformer.transform(source, result);
 
		System.out.println("File saved!");
 
	  } catch (ParserConfigurationException pce) {
		pce.printStackTrace();
	  } catch (TransformerException tfe) {
		tfe.printStackTrace();
	  }
    }
    
    void hochzaehlen(){
	for(int derZaehler=1; derZaehler <= 10; derZaehler++){
		System.out.println("Der Zähler steht bei: "+ derZaehler);
	}

}

    public static void main(String[] args) {
        for(int derZaehler=1; derZaehler <= 10; derZaehler++){
		System.out.println("Der Zähler steht bei: "+ derZaehler);
	}
        XmlManager m = new XmlManager();
                m.read();
                m.updateTest();
                m.read();
                m.writeXmlFile();
                
                //to check: doc.setXmlStandalone(true);
                
    }
}
